package com.wqtz.chechai.view.fragments;

import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.util.TypedValue;
import android.view.View;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.orhanobut.logger.Logger;
import com.wqtz.chechai.R;
import com.wqtz.chechai.adapter.QuoteFirstFragmentAdapter;
import com.wqtz.chechai.base.BaseFragment;
import com.wqtz.chechai.bean.MsgBean;
import com.wqtz.chechai.bean.OrderBean;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.ToastUtils;
import com.wqtz.chechai.view.activities.QuoteDetailActivity;
import com.yinglan.swiperefresh.PullToRefreshBase;
import com.yinglan.swiperefresh.PullToRefreshSwipeListView;
import com.yinglan.swiperefresh.SwipeMenu;
import com.yinglan.swiperefresh.SwipeMenuCreator;
import com.yinglan.swiperefresh.SwipeMenuItem;
import com.yinglan.swiperefresh.SwipeMenuListView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/12 13:38
 */

public class QuoteFirstPagerFragment extends BaseFragment {
	@BindView(R.id.pull_to_refresh_list)
	PullToRefreshSwipeListView mPullRefreshListView;
	QuoteFirstFragmentAdapter adapter;
	private int mHour;
	private int mMinuts;
	List<OrderBean> data;//模拟数据
	int requsetNum = 0;


	@Override
	public BasePresent createPresenter() {
		return null;
	}

	@Override
	protected int getContentView() {
		return R.layout.fragment_quote_first;
	}

	@Override
	protected void init(View view) {
		ButterKnife.bind(this, view);
		long time = System.currentTimeMillis();
		Calendar mCalendar = Calendar.getInstance();
		mCalendar.setTimeInMillis(time);
		mHour = mCalendar.get(Calendar.HOUR);
		mMinuts = mCalendar.get(Calendar.MINUTE);
		data = new ArrayList<>();
		adapter = new QuoteFirstFragmentAdapter(R.layout.item_quote_first);
		createData(15, true);
		mPullRefreshListView.setAdapter(adapter);
		initEvent();


	}


	private void initEvent() {
		adapter.setClickListen(new QuoteFirstFragmentAdapter.QuoteItemClickCallBack() {
			@Override
			public void oItemClickItem(int positon) {
				ToDetail(positon);
			}

			@Override
			public void oItemClickSwipeMenu(int position, int type) {
				switch (type) {
					case 0:
						//ToastUtils.show(getContext(), "删除" + data.get(position).getCarModel(), 2000);
						data.remove(position);
						adapter.clearData();
						adapter.addData(data);
						break;
					case 1:
						//	ToastUtils.show(getContext(), "查看" + data.get(position).getCarModel(), 2000);
						Logger.e("查看" + data.get(position).getCarModel());
						ToDetail(position);
						break;
				}
			}
		});
		mPullRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<SwipeMenuListView>() {
			@Override
			public void onPullDownToRefresh(PullToRefreshBase<SwipeMenuListView> refreshView) {
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						createData(15, true);
						//ToastUtils.show(getContext(), "刷新", 2000);
					}
				}, 2000);

			}

			@Override
			public void onPullUpToRefresh(PullToRefreshBase<SwipeMenuListView> refreshView) {
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						createData(15, false);
						//ToastUtils.show(getContext(), "加载更多", 2000);

					}
				}, 2000);

			}
		});
		mPullRefreshListView.getRefreshableView().setOnMenuItemClickListener(adapter);
		mPullRefreshListView.getRefreshableView().setMenuCreator(creator);


	}


	private void ToDetail(int position) {
		Intent intent = new Intent(getContext(), QuoteDetailActivity.class);
		OrderBean o = data.get(position);
		intent.putExtra("cityId", o.getCityId());
		intent.putExtra("provinceId", o.getProvinceId());
		intent.putExtra("carModelId", o.getCarModelId());
		intent.putExtra("carModel", o.getCarModel());
		intent.putExtra("buyTime", o.getCarBuyTime());
		intent.putExtra("mileage", o.getMileage());
		intent.putExtra("carStatus", o.getCarStatus());
		intent.putExtra("price1", o.getPrice1());
		intent.putExtra("price2", o.getPrice2());
		intent.putExtra("price3", o.getPrice3());
		intent.putExtra("assumPrice", o.getAssumPrice());
		intent.putExtra("VIN", o.getCarVIN());
		intent.putExtra("type", o.getType());
		intent.putExtra("shouldpay", o.getShouldPay());
		intent.putExtra("msgs", (Serializable) o.getMsgs());
		startActivity(intent);
	}

	/**
	 * @param request    请求的条数
	 * @param isResfresh 是否刷新
	 */
	private void createData(int request, boolean isResfresh) {
		data.clear();
		if (isResfresh) {
			requsetNum = 15;
		} else {
			requsetNum += request;
		}
		for (int i = 0; i < requsetNum; i++) {
			//		Logger.e(requsetNum + "请求的数据量");
			OrderBean d = new OrderBean();
			d.setCarVIN("1F2B5D888FA44FRT" + i % 10);
			d.setType(i % 4); //四种状态 0 1 2 3
			d.setVisible(true);//是否可见
			d.setCarModel("广州 雅阁 2.0 EX 2010款" + String.valueOf(i));
			d.setCarBuyTime("6年" + String.valueOf(i % 12) + "个月");
			d.setMileage(String.valueOf(i + 20) + "km");
			d.setCarStatus("一抵");
			d.setAssumPrice(20 + i + "万");
			d.setShouldPay((20 + i - 3) + "万");
			d.setPrice1(20 + i + "");
			d.setPrice2(21 + i + "");
			d.setPrice3(22 + i + "");
			d.setLastModefyTime(mHour + ":" + mMinuts);
			List<MsgBean> msgs = new ArrayList<>();
			for (int j = 0; j < 2; j++) {
				MsgBean msgBean = new MsgBean();
				msgBean.time = "   2016-9-" + i % 30 + " 12:55:12";
				msgBean.title = "【张婷】已审核了该报价，价格调整为" + (10 + i) + "万元";
				msgs.add(msgBean);
			}
			d.setMsgs(msgs);
			data.add(d);
		}
		adapter.clearData();
		adapter.addData(data);
		mPullRefreshListView.onRefreshComplete();
	}

	private SwipeMenuCreator creator = new SwipeMenuCreator() {

		@Override
		public void create(SwipeMenu menu) {
			// Create different menus depending on the view type
			switch (menu.getViewType()) {
				case 0:
					createMenu1(menu);
					break;
				case 1:
					createMenu2(menu);
					break;
			}
		}
	};

	private void createMenu1(SwipeMenu menu) {
		//删除按钮
		SwipeMenuItem item1 = new SwipeMenuItem(
				getContext());
		item1.setBackground(R.mipmap.menu_del);
		item1.setWidth(dp2px(90));
		item1.setTitleColor(Color.WHITE);
		item1.setTitleSize(18);
		item1.setTitle(R.string.delete);
		menu.addMenuItem(item1);

	}

	private void createMenu2(SwipeMenu menu) {
		//查看按钮
		SwipeMenuItem item1 = new SwipeMenuItem(
				getContext());
		item1.setWidth(dp2px(90));
		item1.setBackground(R.mipmap.menu_see);
		item1.setTitleColor(Color.WHITE);
		item1.setTitleSize(18);
		item1.setTitle(R.string.look);
		menu.addMenuItem(item1);
	}


	@Override
	protected void loadDatas() {

	}

	private int dp2px(int dp) {
		return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
				getResources().getDisplayMetrics());
	}


}
